// mycomponent/grabbing-order-detail/grabbing-order-detail.js
const app = getApp()
import { dateRange } from '../../utils/util'
import { travelToolEnum, roomTypeEnum, hotelStarRatedEnum } from '../../utils/dataEnum'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    // 1-导游抢单，2-酒店抢单
    orderType: {
      orderType: Number,
      value: 0
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    id: 0,
    orderData: {},
    loading: false,
    travelToolEnum,
    roomTypeEnum,
    hotelStarRatedEnum
  },

  /**
   * 生命周期
   */
  lifetimes: {
    // 在组件实例进入页面节点树时执行
    attached: function() {
      // 获取导游抢单详情
      // if (this.data.orderType == 1) {
      //   this.getGuideGrabbingDetail()
      // }
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 获取导游抢单详情
     */
    getGuideGrabbingDetail: function(id) {
      if (id) {
        this.setData({ id })
        const urlStr = app.getNetAddresss('plugin.guide.frontend.index.need')
        const _this = this
        wx.showLoading({ title: '加载中...' })
        this.setData({ loading: true })
        app._postNetWork({
          url: urlStr,
          data: { nid: id },
          success: function(res) {
            const resData = res.data
            if (resData.result == 1) {
              let result = resData.data
              // 出行时间
              result.timeRange = dateRange(result.start_at, result.finish_at)
              _this.setData({ orderData: result })
            } else {
              app.tips(resData.msg || '数据获取失败！')
            }
          },
          complete: function() {
            wx.hideLoading()
            _this.setData({ loading: false })
          }
        })
      }
    },

    /**
     * 获取酒店抢单详情
     * @param {String | Number} id 酒店一键升级需求id
     */
    getHotelGrabbingDetail(id) {
      if (id) {
        this.setData({ id })
        const urlStr = app.getNetAddresss('plugin.hotel.frontend.hotel.hotelDemand.get')
        const _this = this
        wx.showLoading({ title: '加载中...' })
        this.setData({ loading: true })
        app._getNetWork({
          url: urlStr,
          data: { id },
          success: function(res) {
            const resData = res.data
            if (resData.result == 1) {
              let result = resData.data
              // 入住时间
              result.timeRange = dateRange(result.start_check_in_time, result.end_check_in_time, true)
              _this.setData({ orderData: result })
            } else {
              app.tips(resData.msg || '数据获取失败！')
            }
          },
          complete: function() {
            wx.hideLoading()
            _this.setData({ loading: false })
          }
        })
      }
    },

    /**
     * 提交
     */
    submit: function() {
      const { orderType, id } = this.data
      const now = new Date()
      const { valid_time } = this.data.orderData
      const valid = new Date(valid_time.replace(/-/g, '/'))
      const timeDiff = valid - now
      if (timeDiff <= 0) {
        app.tips('抢单已结束')
        return
      }
      // 导游抢单
      if (orderType == 1) {
        this.grabbingOrder()
      } 
      // 酒店匹配房间
      else if(orderType == 2) {
        wx.navigateTo({
          url: '/packageFT/hotel/matching-room/matching-room?nid=' + id
        })
      }
    },

    /**
     * 确认抢单
     */
    grabbingOrder: function() {
      const urlStr = app.getNetAddresss('plugin.guide.frontend.need-order-operate.grab')
      const _this = this
      wx.showLoading({
        title: '加载中...',
      })
      app._postNetWork({
        url: urlStr,
        data: {oid: this.data.orderData.order_id},
        success: function(res) {
          const resData = res.data
          if (resData.result == 1) {
            wx.showModal({
              title: '成功',
              content: '抢单成功',
              showCancel: false,
              complete: (resC) => {
                if (resC.confirm) {
                  const { tmplIds } = app.data
                  wx.requestSubscribeMessage({
                    tmplIds: tmplIds[0],
                    complete: function() {
                      wx.redirectTo({
                        url: '/packageFT/guide/order/order',
                      })
                    }
                  })
                }
              }
            })
          } else {
            wx.showModal({
              title: '提示',
              content: resData.msg || '抢单失败！',
              showCancel: false
            })
          }
        },
        complete: function () {
          wx.hideLoading()
        }
      })
    },
  }
})